import * as React from "react";
import Modal from "antd/es/modal/Modal";
import {Button, Card, Col, Form, Input, InputNumber, notification, Row} from "antd";
import {IdcardOutlined, PlusOutlined, UnorderedListOutlined, UserOutlined} from "@ant-design/icons";
import {listPermissions, updateRole} from "../../requests/RoleManageRequest";
import Checkbox from "antd/es/checkbox/Checkbox";


class UpdateRoleModal extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            isModalVisible: false,
            permissionsOptions: [],
            formRef: React.createRef(),
        };
        this.props.onRef(this)
    }


    componentDidMount = () => {
        listPermissions({}).then(edpPermissions => {
            let permissions = edpPermissions.result.map((pDomain) => this.toPermissionTableElement(pDomain))
            this.setState({permissionsOptions: permissions})
        })
    }

    toPermissionTableElement = (permissionDomain) => {
        return {
            "value": permissionDomain.permissionName,
            "label": permissionDomain.displayName,
            "key": permissionDomain.permissionName,
        }
    }

    showModal = () => {
        this.setState({
            isModalVisible: true
        })
    };

    handleCancel = () => {
        this.setState({
            isModalVisible: false
        })
        console.log(this.props.role?.permissions?.map((p) => p.permissionName))
    };

    handleOk = () => {

        if (!this.props.roleName) {
            notification.error({message: "更新角色失败", description: "请在表格中选择角色之后进行更新！"});
            return
        }

        this.state.formRef.current.validateFields()
            .then(values => {
                values['roleName'] = this.props.roleName
                console.log("Begin to update role name with params:" + JSON.stringify(values))
                updateRole(values).then(edpRole => {
                    notification.info({message: "更新角色成功", description: "更新角色" + edpRole.result.roleName + "成功"});
                })
                this.setState({
                    isModalVisible: false
                })
            })
            .catch(info => {
                console.log('Validate message:', info);
            });
    };


    render = () => {
        return (
            <Modal title={"更新角色" + this.props.roleName} visible={this.state.isModalVisible} onOk={this.handleOk}
                   onCancel={this.handleCancel}>
                <Form name="basic" ref={this.state.formRef}
                      initialValues={{'permissions': this.props.role?.permissions?.map((p) => p.permissionName)}}>
                    <Form.Item
                        label="角色显示名称" name="displayName">
                        <Input prefix={<IdcardOutlined/>}/>
                    </Form.Item>

                    <Form.Item
                        label="角色描述信息" name="description">
                        <Input prefix={<UnorderedListOutlined/>}/>
                    </Form.Item>

                    <Form.Item name="permissions" label="权限选择">
                        <Checkbox.Group
                            options={this.state.permissionsOptions}
                        />
                    </Form.Item>
                </Form>
            </Modal>
        );
    }
}

export default UpdateRoleModal